package me.tonywang.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


/**
 *
 */
@ConditionalOnProperty(prefix = "spring.profiles", name = "active", havingValue = "production", matchIfMissing = false)
@Configuration
public class DruidDataSourceConfiguration   {
	protected <T> T createDataSource(DataSourceProperties properties,
			Class<? extends DataSource> type) {
		return (T) properties.initializeDataSourceBuilder().type(type).build();
	}

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource dataSource(DataSourceProperties properties) {
        return createDataSource(properties,DruidDataSource.class) ;
    }
}
